package com.qf.day11;

import java.util.Iterator;
import java.util.Random;

/**
 * 产生10个随机的字符串
 */
public class StrIterator implements Iterator<String> {
    private int count=0;
    private String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private Random random=new Random();
    public static void main(String[] args) {
        // 每个iterator对象，可以被调用10次next()方法返回字符串
        Iterator<String> iterator = new StrIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

    }

    //判断当前位置是否有元素,有元素返回true,无元素返回false
    @Override
    public boolean hasNext() {
        return count<10;
    }

    //获取当前位置的元素,并将迭代器对象向下移一个位置
    @Override
    public String next() {
        StringBuffer sb=new StringBuffer();
        for (int i = 0; i < 10; i++) {
            int randomIndex = random.nextInt(CHARACTERS.length());
            char randomChar = CHARACTERS.charAt(randomIndex);
            sb.append(randomChar);
        }

        count++;
        return sb.toString();
    }
}
